import defaultConfig from './default-option'
//散点图的默认配置项
const scatterDefaultOptions = {
  symbol: 'circle',
  symbolSize: 10,
}
//仪表盘的默认配置项
const gaugeDefaultOptions = {
  radius: '75%',
  clockwise: true,
  title: {
    show: true,
    offsetCenter: [0, '-40%'],
    fontWeight: 'normal',
    fontSize: 18
  },
  detail: {
    show: true,
    fontWeight: 'normal',
    fontSize: 15
  }
}
//圆形图的默认配置项
const pieDefaultOptions = {
  itemStyle: {
    shadowBlur: 10,
    shadowOffsetX: 0,
  },
  radius: '50%',
  center: ['50%', '50%'],
  label: {
    show: true,
    position: 'outside',
    formatter: '{b}',
    color: 'auto',
    fontSize: 12,
    fontWeight: 'normal'
  }
}
//树形图的默认配置项
const treeDefaultOption = {
  left: '10%',
  top: '10%',
  width: '80%',
  height: '80%',
  layout: 'orthogonal',
  orient: 'LR',
  symbol: 'circle',
  symbolSize: 7,
  expandAndCollapse: true,
  roam: false,
  label: {
    fontSize: 12,
    fontWeight: 'normal'
  },
  leaves: {
    label: {
      position: 'right',
      verticalAlign: 'middle',
      align: 'left'
    }
  },
  itemStyle: {
    borderWidth: 1,
    borderType: 'solid'
  }
}
//雷达图的默认配置项
const radarDefaultOption = {
  center: ['50%', '50%'],
  radius: "75%",
  nameGap: 10,

  splitLine: {
    show: true,
  },
  splitArea: {
    show: true,
  },
  indicator: [
    { name: '销售（sales）', max: 6500 },
    { name: '管理（Administration）', max: 16000 },
    { name: '信息技术（Information Techology）', max: 30000 },
    { name: '客服（Customer Support）', max: 38000 },
    { name: '研发（Development）', max: 52000 },
    { name: '市场（Marketing）', max: 25000 }
  ]
}
export default class ECharts extends defaultConfig {
  constructor( type = 'line', otherOption = {}, data, showData = []) {
    //标签配置项
    const label = {
      isUpdate: true,
      show: false,
      position: 'top',
      formatter: '{b}：{c}',
      fontSize: 14
    }
    //配置函数对象集
    const func = {
      setPieOptions: () =>
        this.option.series = this.option.series.map(item =>
          item.type === 'pie' ? this.setNewStyle(pieDefaultOptions, item) : item
        ),
      setScatterOptions: () =>
        this.option.series = this.option.series.map(item =>
          item.type === 'scatter' ? this.setNewStyle(scatterDefaultOptions, item) : item
        ),
      setGaugeOptions: () =>
        this.option.series = this.option.series.map(item =>
          item.type === 'gauge' ? this.setNewStyle(gaugeDefaultOptions, item) : item
        ),
      setRadarOptions: () => {
        item.type === 'radar' ? this.defaultConfig.radar = radarDefaultOption : ''
      },
      setTreeOptions: () =>
        this.option.series = this.option.series.map(item =>
          item.type === 'tree' ? this.setNewStyle(treeDefaultOption, item) : item
        ),
    }
    super()
    const _this = func
    //更改配置项
    data = data.map(item => {
      item.label = item.label ? item.label : label
      return item
    })
    this.option = this.setNewStyle(otherOption, this.defaultConfig)
    this.option.xAxis.data = showData
    this.option.series = data.map(item => {
      let keyArr = Object.keys(item)
      const obj = {}
      for (let i = 0; i < keyArr.length; i++) {
        obj[keyArr[i]] = item[keyArr[i]]
      }
      return obj
    })
    if (typeof (type) == 'string') {
      if (type === 'pie') {
        _this.setPieOptions()
      }
      if (type === 'scatter') {
        _this.setScatterOptions()
      }
      if (type === 'gauge') {
        _this.setGaugeOptions()
      }
      if (type === 'radar') {
        _this.setRadarOptions()
      }
      if (type === 'tree') {
        _this.setTreeOptions()
      }
      this.option.series.forEach(item => {
        item.type = !item.type ? type : item.type
      })
    } else {
      if (data.length !== type.length) {
        alert('data的数组长度与type的数组长度不符')
        return false
      }
      for (let index in type) {
        if (type[index] === 'pie') {
          _this.setPieOptions()
        }
        if (type[index] === 'scatter') {
          _this.setScatterOptions()
        }
        if (type[index] === 'gauge') {
          _this.setGaugeOptions()
        }
        if (type[index] === 'radar') {
          _this.setRadarOptio()
        }
        if (type[index] === 'tree') {
          _this.setTreeOptions()
        }
        this.option.series[index].type = this.option.series[index].type ? this.option.series[index].type : type[index]
      }
    }
  }
  //设置仪表盘显示的名称
  setGaugeName = (option, func) => {
    option.series.forEach(item => {
      item.type === 'gauge' ? item.detail.formatter = func : item
    })
  }
  //设置雷达图的背景样式
  setRadarOption = (option, radar) => option.radar.indicator = radar

}